package org.ciallo.simplemusicplayer.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import org.ciallo.simplemusicplayer.R;
import org.ciallo.simplemusicplayer.data.model.AlbumInfo;

import java.util.Locale;

/**
 * @author Jukomu
 * @Description: 显示专辑列表的 RecyclerView 适配器
 * @Package: org.ciallo.simplemusicplayer.adapter
 * @Project: SimpleMusicPlayer
 * @name: AlbumAdapter
 * @Date: 2025/6/2-23:30
 * @Filename: AlbumAdapter
 */
public class AlbumAdapter extends ListAdapter<AlbumInfo, AlbumAdapter.AlbumViewHolder> {

    private final OnAlbumClickListener listener;
    // private OnAlbumOptionsClickListener optionsClickListener; // 如果需要对整个专辑的更多操作

    public interface OnAlbumClickListener {
        void onAlbumClick(AlbumInfo albumInfo);
    }

    // public interface OnAlbumOptionsClickListener {
    //    void onAlbumOptionsClick(AlbumInfo albumInfo, View anchor);
    // }

    public AlbumAdapter(OnAlbumClickListener listener /*, OnAlbumOptionsClickListener optionsListener */) {
        super(DIFF_CALLBACK);
        this.listener = listener;
        // this.optionsClickListener = optionsListener;
    }

    private static final DiffUtil.ItemCallback<AlbumInfo> DIFF_CALLBACK = new DiffUtil.ItemCallback<AlbumInfo>() {
        @Override
        public boolean areItemsTheSame(@NonNull AlbumInfo oldItem, @NonNull AlbumInfo newItem) {
            // 通常专辑ID是可靠的唯一标识
            return oldItem.albumId == newItem.albumId;
        }

        @Override
        public boolean areContentsTheSame(@NonNull AlbumInfo oldItem, @NonNull AlbumInfo newItem) {
            return oldItem.equals(newItem); // AlbumInfo 需要正确实现 equals
        }
    };

    @NonNull
    @Override
    public AlbumViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_album, parent, false);
        return new AlbumViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull AlbumViewHolder holder, int position) {
        AlbumInfo albumInfo = getItem(position);
        if (albumInfo != null) {
            holder.bind(albumInfo, listener /*, optionsClickListener */);
        }
    }

    static class AlbumViewHolder extends RecyclerView.ViewHolder {
        ImageView ivAlbumCover;
        TextView tvAlbumName;
        TextView tvAlbumArtist;
        TextView tvSongCount;
        ImageButton btnOptions; // 对应 item_album.xml 中的 imageButton_item_album_options

        AlbumViewHolder(View itemView) {
            super(itemView);
            ivAlbumCover = itemView.findViewById(R.id.imageView_item_album_cover);
            tvAlbumName = itemView.findViewById(R.id.textView_item_album_name);
            tvAlbumArtist = itemView.findViewById(R.id.textView_item_album_artist);
            tvSongCount = itemView.findViewById(R.id.textView_item_album_song_count);
            btnOptions = itemView.findViewById(R.id.imageButton_item_album_options);
        }

        void bind(final AlbumInfo albumInfo, final OnAlbumClickListener listener /*, final OnAlbumOptionsClickListener optionsListener */) {
            tvAlbumName.setText(albumInfo.albumName);
            tvAlbumArtist.setText(albumInfo.artistName);
            tvSongCount.setText(String.format(Locale.getDefault(), "%d首歌曲", albumInfo.songCount));

            Context context = itemView.getContext();
            Glide.with(context)
                    .load(albumInfo.albumArtUri != null && !albumInfo.albumArtUri.isEmpty() ? Uri.parse(albumInfo.albumArtUri) : null)
                    .placeholder(R.drawable.default_album_art) // 确保有此默认图
                    .error(R.drawable.default_album_art)
                    .into(ivAlbumCover);

            itemView.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onAlbumClick(albumInfo);
                }
            });

            // 如果启用了对整个专辑的更多操作按钮
            /*
            if (optionsListener != null) {
                btnOptions.setVisibility(View.VISIBLE);
                btnOptions.setOnClickListener(v -> optionsListener.onAlbumOptionsClick(albumInfo, btnOptions));
            } else {
                btnOptions.setVisibility(View.GONE);
            }
            */
        }
    }
}
